//
//  HWGameCenterController.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/3/2.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit
import JXSegmentedView
class HWGameCenterController: HWBaseViewController {
    /// 顶部背景
    var navBgView:UIView!
    /// 搜索背景
    var searchBgView:UIView!
    /// 搜索图
    var searchImageView:UIImageView!
    /// 搜索文字
    var searchPlaceHolder:UILabel!
    /// 搜索
    var searchButton:UIButton!
    /// 铃铛
    var messageButton:UIButton!
    ///标签
    var segmentedView: JXSegmentedView = JXSegmentedView(frame: CGRect.zero)
    var lineView:UIView!
    var segmentedDataSource: JXSegmentedTitleDataSource!
    var listContainerView: JXSegmentedListContainerView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        
        initFrame()
        
    }
}

extension HWGameCenterController {
    private func initUI() -> Void {
        navigationView.isHidden = true
        
        view.backgroundColor = .white
        
        navBgView = UIView()
        navBgView.backgroundColor = .white
        view.addSubview(navBgView)
        
        searchBgView = UIView()
        searchBgView.backgroundColor = .colorWithHexString(hexString: "#F5F5FC")
        searchBgView.layer.cornerRadius = 10*widthScale
        navBgView.addSubview(searchBgView)
        
        searchImageView = UIImageView()
        searchImageView.image = UIImage(named: "feileidesousu")
        searchBgView.addSubview(searchImageView)
        
        searchPlaceHolder = HWLabel.createLabel(title: "请输入关键字进行搜索", color: "#444444", font: 14*widthScale)
        searchBgView.addSubview(searchPlaceHolder)
        
        messageButton = UIButton(type:.custom)
        messageButton.setImage(UIImage(named: "xiaoxi_ico"), for: .normal)
        messageButton.addTarget(self, action: #selector(messageButtonClick(_:)), for: .touchUpInside)
        navBgView.addSubview(messageButton)
        
        segmentedDataSource = JXSegmentedTitleDataSource()
        segmentedDataSource.titles = ["分类","热游榜","新游榜","预约榜","开服表"]
        segmentedDataSource.isTitleColorGradientEnabled = true
        segmentedDataSource.titleNormalColor = UIColor.colorWithHexString(hexString: "#444444")
        segmentedDataSource.titleSelectedColor = UIColor.colorWithHexString(hexString: "#444444")
        segmentedDataSource.titleNormalFont = .systemFont(ofSize: 14*widthScale)
        segmentedDataSource.titleSelectedFont = .boldSystemFont(ofSize: 18*widthScale)
        segmentedView.dataSource = segmentedDataSource
        
        let indicator = JXSegmentedIndicatorImageView()
        indicator.image = UIImage(named: "homeIndicator")
        indicator.indicatorHeight = 4*widthScale
        indicator.indicatorWidth = 36*widthScale
        indicator.contentMode = .scaleAspectFill
        indicator.indicatorCornerRadius = 2
        indicator.layer.masksToBounds = true
        indicator.verticalOffset = 13*widthScale
        segmentedView.indicators = [indicator]
        segmentedView.delegate = self
        segmentedView.backgroundColor = .clear
        view.addSubview(self.segmentedView)
        
        
        listContainerView = JXSegmentedListContainerView(dataSource: self)
        view.addSubview(listContainerView)
        
        segmentedView.listContainer = listContainerView
        segmentedView.backgroundColor = .white
        lineView = UIView()
        lineView.backgroundColor = .colorWithHexString(hexString: "#F7F7F7")
        view.addSubview(lineView)
        
        searchButton = UIButton(type: .custom)
        searchButton.addTarget(self, action: #selector(searchButtonClick(_:)), for: .touchUpInside)
        searchBgView.addSubview(searchButton)
        
    }
    
    private func initFrame() -> Void {
        navBgView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
        }
        
        searchBgView.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.right.equalTo(-60*widthScale)
            make.top.equalTo(49*widthScale)
            make.height.equalTo(34*widthScale)
            make.bottom.equalTo(0)
        }
        
        searchImageView.snp.makeConstraints { make in
            make.left.equalTo(10*widthScale)
            make.centerY.equalToSuperview()
            make.width.equalTo(14*widthScale)
            make.height.equalTo(15*widthScale)
        }
        
        searchPlaceHolder.snp.makeConstraints { make in
            make.left.equalTo(searchImageView.snp.right).offset(9*widthScale)
            make.centerY.equalToSuperview()
        }
        
        searchButton.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        messageButton.snp.makeConstraints { make in
            make.right.equalTo(-20*widthScale)
            make.width.height.equalTo(20*widthScale)
            make.centerY.equalTo(searchBgView)
        }
        
        segmentedView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(navBgView.snp.bottom).offset(5*widthScale)
            make.height.equalTo(44*widthScale)
        }
        lineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(segmentedView.snp.bottom)
            make.height.equalTo(1*widthScale)
        }
        listContainerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(lineView.snp.bottom)
            make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom)
        }
        
        searchButton.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}

extension HWGameCenterController:JXSegmentedListContainerViewDataSource,JXSegmentedViewDelegate {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return segmentedDataSource.dataSource.count
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        if index == 0 {
            let classifyVC = HWGameClassifyController()
            return classifyVC
        }else if index == 1{
            let rankList = HWGameRankController()
            rankList.isHot = true
            return rankList
        }else if index == 2 {
            let rankList = HWGameRankController()
            rankList.isHot = false
            return rankList
        }else if index == 3 {
            let appointment = HWGameAppointmentRankListController()
            return appointment
        }else{
            let openService = HWGameOpenServiceTableController()
            return openService
        }
    }
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        
    }
}

@objc
extension HWGameCenterController {
    func searchButtonClick(_ :UIButton) -> Void {
        let searchController = HWSearchController()
        searchController.modalPresentationStyle = .overFullScreen
        self.present(searchController, animated: true, completion: nil)
    }
    
    func messageButtonClick(_ :UIButton) -> Void {
        if HWLocallyStoredData.getLoginUserID().count>0{
            let message = HWXiaoXiMessageCenterViewController()
            self.navigationController?.pushViewController(message, animated: true)
        }else {
            let loginController = HWLoginController()
            self.navigationController?.pushViewController(loginController, animated: true)
        }
    }
}
